//
//  SSSegmentView.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/5/30.
//

import UIKit

public typealias SSSegmentViewClosure = (_ index: Int)->()

class SSSegmentView: UIView {
    
    var resultClosure: SSSegmentViewClosure?     // 闭包
    var lastSegmentView = SSSegmentItemView()
    var items = [SSSegmentItemView]()
    
    var lastSegmentView2 = SSSegmentItemView2()
    var items2 = [SSSegmentItemView2]()
    
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        if frame.isEmpty {self.frame = CGRect(x: 0, y: 0, width: 110, height: 30)}
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
    }

    func setupUI()
    {
        segmentChildrenViews(superView: self)
    }
    
    func mealResult(_ finished: @escaping SSSegmentViewClosure) {
        resultClosure = finished
    }
    
    func segmentChildrenViews(superView:UIView) {
        
        let details = ["包月","单次","包年","宠物"]
        let width = ScreenWidth/CGFloat(details.count)
        let height = CGFloat(46)
        for (i, item) in details.enumerated() {

            let bgView = SSSegmentItemView(frame: CGRect(x: width * CGFloat(i), y: 0, width: width, height: height))
            bgView.tag = 1000 + i
            bgView.textL.text = item
            superView.addSubview(bgView)
            superView.backgroundColor = UIColor(hex: "#197FD6")
            bgView.isUserInteractionEnabled = true
            let tapGes = UITapGestureRecognizer(target: self, action: #selector(tapedTitleLabel(_:)))
            bgView.addGestureRecognizer(tapGes)
            
            if(i == 0){
//                lastSegmentView = bgView
//                tapedTitleLabel(tapGes)
                setupSingleItem(item: bgView)
            }
            items.append(bgView)
        }
    }
    
    @objc private func tapedTitleLabel(_ tapGes : UITapGestureRecognizer) {

        cancelSingleItem(item: lastSegmentView)
         
        let segmentV = tapGes.view as! SSSegmentItemView
        setupSingleItem(item: segmentV)
        resultClosure?(segmentV.tag -  1000)
 
    }
    
    func setupSelectedItem(index:Int) {
        for(i,item) in items.enumerated(){
            if(item.isSelected){
                cancelSingleItem(item: item)
            }
            if(index == i){
                setupSingleItem(item: item)
            }
        }
    }
    
    
    func setupSingleItem(item:SSSegmentItemView) {
        item.backgroundColor = UIColor.white
        item.textL.textColor =  UIColor(hex: "#103573")
        item.sliderView.isHidden = false
        lastSegmentView = item
        item.isSelected = true
    }
    
    func cancelSingleItem(item:SSSegmentItemView) {
        item.backgroundColor = UIColor(hex: "#197FD6")
        item.textL.textColor = UIColor.white
        item.sliderView.isHidden = true
        item.isSelected = false
    }
    
    
    
    
    
    
    
    
    
    
    func setupUI2()
    {
        layer.borderColor = UIColor.white.cgColor
        layer.masksToBounds = true
        layer.cornerRadius = 10
        layer.borderWidth = 1
        self.backgroundColor = UIColor(hex: "#F4FAFF")
        segmentChildrenViews2(segmentBGView: self)
    }
    
    func segmentChildrenViews2(segmentBGView:UIView) {
        
        let details = ["服务内容","图文详情","常见问题"]
        let width = segmentBGView.frame.width/CGFloat(details.count)
        let height = segmentBGView.frame.height
        for (i, item) in details.enumerated() {
            
    
            let segmentView:SSSegmentItemView2 = SSSegmentItemView2(frame: CGRect(x: width * CGFloat(i), y: 0, width: width, height: height))
            segmentBGView.addSubview(segmentView)
            segmentView.textL.text = item
            segmentView.textL.textColor = UIColor(hex: "#6D798B")
            segmentView.backgroundColor = UIColor(hex: "#F4FAFF")
            let tapGes = UITapGestureRecognizer(target: self, action: #selector(tapedTitleLabel2(_:)))
            segmentView.tag = 1000 + i
            segmentView.addGestureRecognizer(tapGes)

            items2.append(segmentView)
            if(i == 0){
//                lastSegmentView2 = segmentView
//                tapedTitleLabel2(tapGes)
                setupSingleItem2(item: segmentView)
            }
        }
    }
    
    @objc private func tapedTitleLabel2(_ tapGes : UITapGestureRecognizer) {
            
        cancelSingleItem2(item: lastSegmentView2)
        let segmentV = tapGes.view as! SSSegmentItemView2
        setupSingleItem2(item: segmentV)
        resultClosure?(segmentV.tag -  1000)
    }
    
    func setupSelectedItem2(index:Int) {
        for(i,item) in items2.enumerated(){
            if(item.isSelected){
                cancelSingleItem2(item: item)
            }
            if(index == i){
                setupSingleItem2(item: item)
            }
        }
    }
    
    
    func setupSingleItem2(item:SSSegmentItemView2) {
//        item.backgroundColor = UIColor.white
        item.textL.textColor = UIColor(hex: "#14233D")
//        item.layer.cornerRadius = 10
        item.sliderView.isHidden = false
        item.isSelected = true
        lastSegmentView2 = item

    }
    
    func cancelSingleItem2(item:SSSegmentItemView2) {
//        item.backgroundColor = UIColor(hex: "#F4FAFF")
        item.textL.textColor = UIColor(hex: "#6D798B")
//        item.layer.cornerRadius = 0
        item.sliderView.isHidden = true
        item.isSelected = false
    }


}

